package com.bluesky.jwf.component;

import com.bluesky.jwf.component.event.EventHandler;

public class CheckBox extends Component {
	public static String EVENT_CLICK = "click";

	private com.bluesky.javawebbrowser.domain.html.tags.form.input.CheckBox checkBoxTag;

	@Override
	public void init(String html) {

		super.init(html);

		checkBoxTag = (com.bluesky.javawebbrowser.domain.html.tags.form.input.CheckBox) loadByJwfClass("checkbox");

	}

	@Override
	public void setId(String id) {
		super.setId(id);
		checkBoxTag.setName(getId() + "_checkbox");

	}

	@Override
	public void setPostBackData(String name, String value) {
		super.setPostBackData(name, value);

		/**
		 * in a html tag of checkbox, value is post back. but the value for a
		 * jwf component means nothing. we only care if it is checked or not. if
		 * any value is post back, means it is checked.
		 */
		if (name.equals("checkbox"))
			checkBoxTag.setChecked(true);
	}

	public boolean isChecked() {
		return checkBoxTag.isChecked();
	}

	public void setChecked(boolean value) {
		checkBoxTag.setChecked(value);
	}

	public void setClickHandler(EventHandler handler) {

		if (handler != null) {
			checkBoxTag.setOnClick(String.format(
					"_oum_post(this,'%s','click')", getId()));
		} else
			checkBoxTag.setOnClick("");

		eventHandlers.put(EVENT_CLICK, handler);
	}
}
